﻿using Newtonsoft.Json;
using SuperSocket.SocketBase;
using SuperWebSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharedModule
{
    public class MockSocketServer : IMockSocketServer
    {
        public WebSocketServer server;
        public delegate void NewMessageReceived(WebSocketSession session, string message);

        public MockSocketServer(int port)
        {
            this.server = new WebSocketServer();
            this.Setup(port);
        }
        private void Setup(int port)
        {
            if (!this.server.Setup(port))
            {
                LogUtility.Log.Fatal(String.Format("Server stepup failed: The port is {0}", port));
                return;
            }
        }

        public void Start()
        {
            if (!this.server.Start())
            {
                LogUtility.Log.Fatal("Server start failed");
                return;
            }
        }

        public void Stop()
        {
            this.server.Stop();
            LogUtility.Log.Warn("Server stopped");
        }

        public int GetSessionCount()
        {
            return this.server.SessionCount;
        }

        public WebSocketSession GetSessionById(string sessionId)
        {
            return this.server.GetSessionByID(sessionId);
        }

        public void BroadCast(MessageObject message)
        {
            this.server.Broadcast(this.server.GetAllSessions(), JsonConvert.SerializeObject(message), (session, flag) => { });
        }
        public void BroadCast(MessageObject message, List<WebSocketSession> sessions)
        {
            this.server.Broadcast(sessions, JsonConvert.SerializeObject(message), (session, flag) => { });
        }
        public void BroadCast(MessageObject message, List<WebSocketSession> sessions, Action<WebSocketSession, bool> callback)
        {
            this.server.Broadcast(sessions, JsonConvert.SerializeObject(message), callback);
        }

        #region event register

        public void AddCustomMessageHandler(Action<WebSocketSession, string> messageHandler)
        {
            this.server.NewMessageReceived += new SessionHandler<WebSocketSession, string>(messageHandler);
        }

        private void RemoveCustomMessageHander(Action<WebSocketSession, string> action)
        {
            this.server.NewMessageReceived -= new SessionHandler<WebSocketSession, string>(action);
        }

        public void AddCustomDataHandler(Action<WebSocketSession, byte[]> dataHander)
        {
            this.server.NewDataReceived += new SessionHandler<WebSocketSession, byte[]>(dataHander);
        }

        private void RemoveCustomDataHander(Action<WebSocketSession, byte[]> dataHandler)
        {
            this.server.NewDataReceived -= new SessionHandler<WebSocketSession, byte[]>(dataHandler);
        }

        public void AddCustomSessionConnectHandler(Action<WebSocketSession> connectHandler)
        {
            this.server.NewSessionConnected += new SessionHandler<WebSocketSession>(connectHandler);
        }

        private void RemoveCustomSessionConnectHandler(Action<WebSocketSession> connectHandler)
        {
            this.server.NewSessionConnected -= new SessionHandler<WebSocketSession>(connectHandler);
        }

        public void AddCustomSessionCloseHandler(Action<WebSocketSession, CloseReason> closeHandler)
        {
            this.server.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(closeHandler);
        }

        public void RemoveCustomSessionCloseHandler(Action<WebSocketSession, CloseReason> closeHandler)
        {
            this.server.SessionClosed -= new SessionHandler<WebSocketSession, CloseReason>(closeHandler);
        }

        #endregion
    }
}
